<?php
//dezend by http://www.yunlu99.com/ QQ:270656184
class CI_Config
{
	/**
	 * List of all loaded config values
	 *
	 * @var	array
	 */
	public $config = array();
	/**
	 * List of all loaded config files
	 *
	 * @var	array
	 */
	public $is_loaded = array();
	/**
	 * List of paths to search when trying to load a config file.
	 *
	 * @used-by	CI_Loader
	 * @var		array
	 */
	public $_config_paths = array(APPPATH);

	public function __construct()
	{
		$this->config = &get_config();

		if (empty($this->config['base_url'])) {
			if (isset($_SERVER['SERVER_ADDR'])) {
				if (strpos($_SERVER['SERVER_ADDR'], ':') !== false) {
					$server_addr = '[' . $_SERVER['SERVER_ADDR'] . ']';
				}
				else {
					$server_addr = $_SERVER['SERVER_ADDR'];
				}

				$base_url = (is_https() ? 'https' : 'http') . '://' . $server_addr . substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME'])));
			}
			else {
				$base_url = 'http://localhost/';
			}

			$this->set_item('base_url', $base_url);
		}

		log_message('info', 'Config Class Initialized');
	}

	public function load($file = '', $use_sections = false, $fail_gracefully = false)
	{
		$file = ($file === '' ? 'config' : str_replace('.php', '', $file));
		$loaded = false;

		foreach ($this->_config_paths as $path) {
			foreach (array($file, ENVIRONMENT . DIRECTORY_SEPARATOR . $file) as $location) {
				$file_path = $path . 'config/' . $location . '.php';

				if (in_array($file_path, $this->is_loaded, true)) {
					return true;
				}

				if (!file_exists($file_path)) {
					continue;
				}

				include $file_path;
				if (!isset($config) || !is_array($config)) {
					if ($fail_gracefully === true) {
						return false;
					}

					show_error('Your ' . $file_path . ' file does not appear to contain a valid configuration array.');
				}

				if ($use_sections === true) {
					$this->config[$file] = isset($this->config[$file]) ? array_merge($this->config[$file], $config) : $config;
				}
				else {
					$this->config = array_merge($this->config, $config);
				}

				$this->is_loaded[] = $file_path;
				$config = NULL;
				$loaded = true;
				log_message('debug', 'Config file loaded: ' . $file_path);
			}
		}

		if ($loaded === true) {
			return true;
		}
		else if ($fail_gracefully === true) {
			return false;
		}

		show_error('The configuration file ' . $file . '.php does not exist.');
	}

	public function item($item, $index = '')
	{
		if ($index == '') {
			return isset($this->config[$item]) ? $this->config[$item] : NULL;
		}

		return isset($this->config[$index]) && isset($this->config[$index][$item]) ? $this->config[$index][$item] : NULL;
	}

	public function slash_item($item)
	{
		if (!isset($this->config[$item])) {
			return NULL;
		}
		else if (trim($this->config[$item]) === '') {
			return '';
		}

		return rtrim($this->config[$item], '/') . '/';
	}

	public function site_url($uri = '', $protocol = NULL)
	{
		$base_url = $this->slash_item('base_url');

		if (isset($protocol)) {
			if ($protocol === '') {
				$base_url = substr($base_url, strpos($base_url, '//'));
			}
			else {
				$base_url = $protocol . substr($base_url, strpos($base_url, '://'));
			}
		}

		if (empty($uri)) {
			return $base_url . $this->item('index_page');
		}

		$uri = $this->_uri_string($uri);

		if ($this->item('enable_query_strings') === false) {
			$suffix = (isset($this->config['url_suffix']) ? $this->config['url_suffix'] : '');

			if ($suffix !== '') {
				if (($offset = strpos($uri, '?')) !== false) {
					$uri = substr($uri, 0, $offset) . $suffix . substr($uri, $offset);
				}
				else {
					$uri .= $suffix;
				}
			}

			return $base_url . $this->slash_item('index_page') . $uri;
		}
		else if (strpos($uri, '?') === false) {
			$uri = '?' . $uri;
		}

		return $base_url . $this->item('index_page') . $uri;
	}

	public function base_url($uri = '', $protocol = NULL)
	{
		$base_url = $this->slash_item('base_url');

		if (isset($protocol)) {
			if ($protocol === '') {
				$base_url = substr($base_url, strpos($base_url, '//'));
			}
			else {
				$base_url = $protocol . substr($base_url, strpos($base_url, '://'));
			}
		}

		return $base_url . $this->_uri_string($uri);
	}

	protected function _uri_string($uri)
	{
		if ($this->item('enable_query_strings') === false) {
			is_array($uri) && ($uri = implode('/', $uri));
			return ltrim($uri, '/');
		}
		else if (is_array($uri)) {
			return http_build_query($uri);
		}

		return $uri;
	}

	public function system_url()
	{
		$x = explode('/', preg_replace('|/*(.+?)/*$|', '\\1', BASEPATH));
		return $this->slash_item('base_url') . end($x) . '/';
	}

	public function set_item($item, $value)
	{
		$this->config[$item] = $value;
	}
}

defined('BASEPATH') || exit('No direct script access allowed');

?>
